package com.elock.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Repository;

import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.dao.impl.DaoCenter;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.DateUtil;
import com.elock.base.core.util.WebUtils;

/**
 * Dao - PcRole
 * @author 仁德
 */
@Repository("pcRoleBaseDao")
public class PcRoleBaseDao extends DaoCenter {

	public Page<Map<String, Object>> findPage(String name, Boolean isSystem,
			String firstTime, String lastTime, Pageable pageable) {

		List<Object> list = new ArrayList<Object>();
		StringBuilder sql = new StringBuilder();
		Long companyInfoId=WebUtils.getCurrentCompanyInfoId();
		sql.append("select r.* from pc_role r where 1 = 1");
		if (companyInfoId != null) {
			sql.append(" and r.company_info_id=?");
			list.add(companyInfoId);
		}
		if (!ConvertUtil.isEmpty(name)) {
			sql.append(" and r.name like ?");
			list.add("%" + name + "%");
		}
		if (isSystem != null) {
			if (isSystem) {
				sql.append(" and r.is_system = 1");
			}
			else {
				sql.append(" and r.is_system = 0");
			}
		}
		if (!ConvertUtil.isEmpty(firstTime)) {
			sql.append(" and r.create_date >= ?");
			list.add(DateUtil.convert(firstTime + " 00:00:00"));
		}
		if (!ConvertUtil.isEmpty(lastTime)) {
			sql.append(" and r.create_date < ?");
			list.add(DateUtils.addDays(DateUtil.convert(lastTime + " 00:00:00"), 1));
		}
		sql.append(" order by r.create_date desc");

		Object[] objs = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			objs[i] = list.get(i);
		}

		Page<Map<String, Object>> page = getNativeDao().findPageMap(sql.toString(),
				objs,
				pageable);

		return page;
	}
}